{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内共有 168 个素数。\n",
      "这些素数为： [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def get_day_of_year(year, month, day):\n",
    "    import datetime\n",
    "    date = datetime.datetime(year, month, day)\n",
    "    day_of_year = date.timetuple().tm_yday\n",
    "    return day_of_year\n",
    "\n",
    "def is_prime(n):\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    if n == 2:\n",
    "        return True\n",
    "    if n % 2 == 0:\n",
    "        return False\n",
    "    for i in range(3, int(n**0.5) + 1, 2):\n",
    "        if n % i == 0:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "def find_primes(limit):\n",
    "    primes = []\n",
    "    for num in range(2, limit + 1):\n",
    "        if is_prime(num):\n",
    "            primes.append(num)\n",
    "    return primes\n",
    "\n",
    "def grade_score(score):\n",
    "    grade = 'A' if score >= 90 else ('B' if score >= 60 else 'C')\n",
    "    return grade\n",
    "\n",
    "def count_characters(input_string):\n",
    "    letters = 0\n",
    "    spaces = 0\n",
    "    digits = 0\n",
    "    others = 0\n",
    "    for char in input_string:\n",
    "        if char.isalpha():  # 判断是否为字母\n",
    "            letters += 1\n",
    "        elif char.isspace():  # 判断是否为空格\n",
    "            spaces += 1\n",
    "        elif char.isdigit():  # 判断是否为数字\n",
    "            digits += 1\n",
    "        else:  # 其他字符\n",
    "            others += 1\n",
    "    return letters, spaces, digits, others\n",
    "\n",
    "def find_perfect_numbers(limit):\n",
    "    perfect_numbers = []\n",
    "    for num in range(1, limit + 1):\n",
    "        sum_of_factors = 0\n",
    "        for factor in range(1, num // 2 + 1):\n",
    "            if num % factor == 0:\n",
    "                sum_of_factors += factor\n",
    "        if sum_of_factors == num:\n",
    "            perfect_numbers.append(num)\n",
    "    return perfect_numbers\n",
    "\n",
    "def factorial(n):\n",
    "    if n == 0 or n == 1:\n",
    "        return 1\n",
    "    result = 1\n",
    "    for i in range(2, n + 1):\n",
    "        result *= i\n",
    "    return result\n",
    "\n",
    "def sum_of_factorials(N):\n",
    "    total_sum = 0\n",
    "    for i in range(1, N + 1):\n",
    "        total_sum += factorial(i)\n",
    "    return total_sum\n",
    "\n",
    "def print_diamond(N):\n",
    "    if N % 2 == 0 or N <= 0:\n",
    "        print(\"请输入一个大于0的奇数。\")\n",
    "        return\n",
    "    for i in range(N // 2 + 1):\n",
    "        spaces = ' ' * (N // 2 - i)\n",
    "        stars = '*' * (2 * i + 1)\n",
    "        print(spaces + stars)\n",
    "    for i in range(N // 2 - 1, -1, -1):\n",
    "        spaces = ' ' * (N // 2 - i)\n",
    "        stars = '*' * (2 * i + 1)\n",
    "        print(spaces + stars)\n",
    "\n",
    "def sort_numbers():\n",
    "    numbers = []\n",
    "    for i in range(10):\n",
    "        num = int(input(f\"请输入第{i+1}个数: \"))\n",
    "        numbers.append(num)\n",
    "    sorted_numbers = sorted(numbers)\n",
    "    print(\"排序后的结果:\", sorted_numbers)\n",
    "\n",
    "def find_insert_position(sorted_array, new_num):\n",
    "    for i in range(len(sorted_array)):\n",
    "        if new_num <= sorted_array[i]:\n",
    "            return i\n",
    "    return len(sorted_array)\n",
    "\n",
    "def insert_sorted_array(sorted_array, new_num):\n",
    "    insert_pos = find_insert_position(sorted_array, new_num)\n",
    "    sorted_array.insert(insert_pos, new_num)\n",
    "    return sorted_array\n",
    "\n",
    "def josephus_problem(n):\n",
    "    people = list(range(1, n + 1))\n",
    "    current_index = 0\n",
    "    while len(people) > 1:\n",
    "        current_index = (current_index + 2) % len(people)\n",
    "        people.pop(current_index)\n",
    "    return people[0]\n",
    "\n",
    "def main():\n",
    "    try:\n",
    "        user_input = int(input(\"请输入一个1到10之间的整数: \"))\n",
    "        \n",
    "        if 1 <= user_input <= 10:\n",
    "            if user_input == 1:\n",
    "                year = int(input(\"请输入年份: \"))\n",
    "                month = int(input(\"请输入月份: \"))\n",
    "                day = int(input(\"请输入日期: \"))\n",
    "                day_number = get_day_of_year(year, month, day)\n",
    "                print(f\"{year}年{month}月{day}日是这一年的第 {day_number} 天。\")\n",
    "            elif user_input == 2:\n",
    "                limit = 1000\n",
    "                primes = find_primes(limit)\n",
    "                print(f\"1000以内共有 {len(primes)} 个素数。\")\n",
    "                print(\"这些素数为：\", primes)\n",
    "            elif user_input == 3:\n",
    "                score = int(input(\"请输入你的成绩：\"))\n",
    "                grade = grade_score(score)\n",
    "                print(f\"你的成绩等级为：{grade}\")\n",
    "            elif user_input == 4:\n",
    "                input_string = input(\"请输入一行字符串：\")\n",
    "                letters, spaces, digits, others = count_characters(input_string)\n",
    "                print(f\"字母个数：{letters}\")\n",
    "                print(f\"空格个数：{spaces}\")\n",
    "                print(f\"数字个数：{digits}\")\n",
    "                print(f\"其他字符个数：{others}\")\n",
    "            elif user_input == 5:\n",
    "                perfect_numbers = find_perfect_numbers(1000)\n",
    "                print(\"1000以内的所有完数：\")\n",
    "                for number in perfect_numbers:\n",
    "                    print(number)\n",
    "            elif user_input == 6:\n",
    "                N = int(input(\"请输入一个正整数N: \"))\n",
    "                if N < 0:\n",
    "                    print(\"请输入一个非负整数。\")\n",
    "                else:\n",
    "                    result = sum_of_factorials(N)\n",
    "                    print(f\"1! + 2! + 3! + ... + {N}! 的值是: {result}\")\n",
    "            elif user_input == 7:\n",
    "                N = int(input(\"请输入一个奇数N: \"))\n",
    "                if N % 2 == 0 or N <= 0:\n",
    "                    print(\"请输入一个大于0的奇数。\")\n",
    "                else:\n",
    "                    print_diamond(N)\n",
    "            elif user_input == 8:\n",
    "                sort_numbers()\n",
    "            elif user_input == 9:\n",
    "                sorted_array = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]\n",
    "                new_num = int(input(\"请输入要插入的新数字: \"))\n",
    "                sorted_array = insert_sorted_array(sorted_array, new_num)\n",
    "                print(\"插入新数字后的数组:\", sorted_array)\n",
    "            elif user_input == 10:\n",
    "                n = int(input(\"请输入总人数: \"))\n",
    "                last_person = josephus_problem(n)\n",
    "                print(f\"最后留下的那个人是原来第 {last_person} 号。\")\n",
    "        else:\n",
    "            print(\"输入的数字不在1到10之间，请重新输入。\")\n",
    "    except ValueError:\n",
    "        print(\"无效输入，请输入一个1到10之间的整数！\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
